#include "CountryForm.h"
#include "NavigationManager.h"
#include "OpennavUtils.h"

using namespace Osp::App;
using namespace Osp::Base;
using namespace Osp::Base::Collection;
using namespace Osp::Ui;
using namespace Osp::Ui::Controls;
using namespace Osp::Graphics;
using namespace Osp::Io;
using namespace Osp::Text;

CountryForm::CountryForm()
	: __pSearchService(null), __pCurrentResult(null), __pLock(null) { }

result CountryForm::Construct(OpennavSearchServiceProvider* searchService) {
	Form::Construct(L"IDF_SELECT_COUNTRY");
	__pSearchService = searchService;
	return E_SUCCESS;
}

result CountryForm::OnInitializing() {
	result r = E_SUCCESS;
	__pScrollPanel = static_cast<ScrollPanel *>(GetControl(L"IDC_SCROLLPANEL1", false));
	__pListCountry = static_cast<SlidableList *>(GetControl(L"IDPC_LIST_COUNTRY", true));
	if (__pListCountry != null) {
		__pListCountry->AddSlidableListEventListener(*this);
		__pListCountry->AddCustomItemEventListener(*this);
	}
	__pSearchCountry = static_cast<EditField *>(GetControl(L"IDPC_SEARCH_COUNTRY", true));
	if (__pSearchCountry != null) {
		__pSearchCountry->AddTextEventListener(*this);
		__pSearchCountry->AddActionEventListener(*this);
		__pSearchCountry->SetOverlayKeypadCommandButton(COMMAND_BUTTON_POSITION_RIGHT, L"Close", ID_BUTTON_EDITFIELD_CLOSE);
	}
	__pCustomListItemFormat = new CustomListItemFormat();
	__pCustomListItemFormat->Construct();
	__pCustomListItemFormat->AddElement(COUNTRY_NAME_ID, Osp::Graphics::Rectangle(10, 30, 460, 55), 40);
	SetSoftkeyActionId(SOFTKEY_1, ID_SOFTKEY_BACK);
	AddSoftkeyActionListener(SOFTKEY_1, *this);
	__pLock = new Mutex();
	__pLock->Create();
	return r;
}

result CountryForm::OnTerminating() {
	result r = E_SUCCESS;
	delete __pCustomListItemFormat;
	delete __pLock;
	return r;
}

void CountryForm::OnActionPerformed(const Osp::Ui::Control& source, int actionId) {
	switch (actionId) {
		case ID_BUTTON_EDITFIELD_CLOSE:
			{
				__pScrollPanel->CloseOverlayWindow();
				__pSearchCountry->Draw();
				__pSearchCountry->Show();
			}
			break;
		case ID_SOFTKEY_BACK:
			{
				Frame *pFrame = Application::GetInstance()->GetAppFrame()->GetFrame();
				NavigationManager *pNavManager = static_cast<NavigationManager *>(pFrame->GetControl(NavigationManager::FORM_MANAGER_NAME));
				if (pNavManager != null) {
					pNavManager->SendUserEvent(NavigationManager::REQUEST_ID_MAINMENU_FORM, null);
				}
			}
			break;
		default:
			break;
	}
}

void CountryForm::OnTextValueChanged(const Osp::Ui::Control& source) {
	PerformSearch();
}

void CountryForm::OnLoadToTopRequested(const Osp::Ui::Control &source, int index, int numItems)
{
	if (__pListCountry != null) {
		__pLock->Acquire();
		for(int i = index; i > index - numItems; i--) {
			opennav::Country country;
			__pCurrentResult->GetResult(i, country);
			String countryName;
			OpennavUtils::Convert(country.name, countryName);
			CustomListItem* pItem = new CustomListItem();
			pItem->Construct(100);
			pItem->SetItemFormat(*__pCustomListItemFormat);
			pItem->SetElement(COUNTRY_NAME_ID, countryName);
			__pListCountry->LoadItemToTop(*pItem, country.country_id);
		}
		__pLock->Release();
	}
}

void CountryForm::OnLoadToBottomRequested (const Osp::Ui::Control &source, int index, int numItems)
{
	if (__pListCountry != null) {
		__pLock->Acquire();
		for(int i = index; i < index + numItems; i++) {
			opennav::Country country;
			__pCurrentResult->GetResult(i, country);
			String countryName;
			OpennavUtils::Convert(country.name, countryName);
			CustomListItem* pItem = new CustomListItem();
			pItem->Construct(100);
			pItem->SetItemFormat(*__pCustomListItemFormat);
			pItem->SetElement(COUNTRY_NAME_ID, countryName);
			__pListCountry->LoadItemToBottom(*pItem, country.country_id);
		}
		__pLock->Release();
	}
}

void CountryForm::OnItemStateChanged(const Osp::Ui::Control &source, int index, int itemId, Osp::Ui::ItemStatus status) {
	Frame *pFrame = Application::GetInstance()->GetAppFrame()->GetFrame();
	NavigationManager *pNavManager = static_cast<NavigationManager *>(pFrame->GetControl(NavigationManager::FORM_MANAGER_NAME));
	if (pNavManager != null) {
		ArrayList* args = new ArrayList();
		args->Construct();
		args->Add(*(new Integer(itemId)));
		pNavManager->SendUserEvent(NavigationManager::REQUEST_ID_ADDRESS_CITY_BY_COUNTRY, args);
	}
}

void CountryForm::OnUserEventReceivedN(RequestId requestId, IList* pArgs) {
	switch(requestId)
	{
		case REQUEST_ID_RESET:
			{
				__pSearchCountry->SetText(Osp::Base::String(""));
				PerformSearch();
			}
			break;
		default:
			break;
	}
}

void CountryForm::OnGeocodingRequestResultReceived(IOpennavSearchResult* result) {
	AppLogDebug("search country request callback");
	if (result->GetRequestType() == SEARCH_COUNTRY) {
		if (__pCurrentResult != null) {
			delete __pCurrentResult;
		}
		__pCurrentResult = (OpennavSearchCountryResult*)result;
		__pListCountry->RemoveAllItems();
		__pListCountry->SetItemCountAndHeight(__pCurrentResult->GetCount(), __pCurrentResult->GetCount() * 100);
		__pListCountry->Draw();
		__pListCountry->Show();
		RequestRedraw(true);
	}
}

void CountryForm::PerformSearch() {
	String text = __pSearchCountry->GetText();
	__pLock->Acquire();
	__pListCountry->RemoveAllItems();
	if (__pCurrentResult != null) {
		delete __pCurrentResult;
		__pCurrentResult = null;
	}
	__pListCountry->SetItemCountAndHeight(0, 0);
	__pLock->Release();
	OpennavSearchCountryRequest request = OpennavSearchCountryRequest(text);
	__pSearchService->Search(request, *this);
}

